package com.ruoyi.fetv.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fetv.controller.userController.information.vo.RespVO;
import com.ruoyi.fetv.controller.vo.Label;
import com.ruoyi.fetv.controller.vo.information.AddReq;
import com.ruoyi.fetv.controller.vo.information.EditReq;
import com.ruoyi.fetv.domain.FetvInformation;
import com.ruoyi.fetv.mapper.FetvCommentMapper;
import com.ruoyi.fetv.mapper.FetvInformationMapper;
import com.ruoyi.fetv.service.IFetvInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资讯Service业务层处理
 *
 * @author Jack
 * @date 2023-03-07
 */
@Service
public class FetvInformationServiceImpl implements IFetvInformationService {
    @Autowired
    private FetvInformationMapper fetvInformationMapper;
    @Autowired
    private FetvCommentMapper fetvCommentMapper;

    /**
     * 查询资讯
     *
     * @param id 资讯主键
     * @return 资讯
     */
    @Override
    public FetvInformation selectFetvInformationById(Long id) {
        FetvInformation fetvInformation = fetvInformationMapper.selectFetvInformationById(id);
        if (fetvInformation == null) {
            return null;
        }
        fetvInformation.setComments(fetvCommentMapper.getInformationCommentCount(String.valueOf(fetvInformation.getId())));
        return fetvInformation;
    }

    /**
     * 查询资讯列表
     *
     * @param fetvInformation 资讯
     * @return 资讯
     */
    @Override
    public List<FetvInformation> selectFetvInformationList(FetvInformation fetvInformation) {
        List<FetvInformation> fetvInformations = fetvInformationMapper.selectFetvInformationList(fetvInformation);
        for (FetvInformation information : fetvInformations) {
            information.setComments(fetvCommentMapper.getInformationCommentCount(String.valueOf(information.getId())));
        }
        return fetvInformations;
    }

    /**
     * 新增资讯
     *
     * @param fetvInformation 资讯
     * @return 结果
     */
    @Override
    public int insertFetvInformation(AddReq fetvInformation) {
        fetvInformation.setCreateTime(DateUtils.getNowDate());
        return fetvInformationMapper.insertFetvInformation(fetvInformation);
    }

    /**
     * 修改资讯
     *
     * @param fetvInformation 资讯
     * @return 结果
     */
    @Override
    public int updateFetvInformation(EditReq fetvInformation) {
        fetvInformation.setUpdateTime(DateUtils.getNowDate());
        return fetvInformationMapper.updateFetvInformation(fetvInformation);
    }

    /**
     * 批量删除资讯
     *
     * @param ids 需要删除的资讯主键
     * @return 结果
     */
    @Override
    public int deleteFetvInformationByIds(Long[] ids) {
        return fetvInformationMapper.deleteFetvInformationByIds(ids);
    }

    /**
     * 删除资讯信息
     *
     * @param id 资讯主键
     * @return 结果
     */
    @Override
    public int deleteFetvInformationById(Long id) {
        return fetvInformationMapper.deleteFetvInformationById(id);
    }

    @Override
    public List<RespVO> selectFetvInformationTitleList(FetvInformation fetvInformation) {
        return fetvInformationMapper.selectFetvInformationTitleList(fetvInformation);
    }

    @Override
    public String getNameById(String id) {
        return fetvInformationMapper.getTitleById(id);
    }

    /**
     * 获得标签列表
     *
     * @param name 标题前缀
     * @return 标签列表
     */
    @Override
    public List<Label> getIdAndNameByName(String name) {
        return fetvInformationMapper.getIdAndNameByName(name);
    }
}
